const Tag = require('../db').Tag

module.exports = {
    async getTag(ctx,next) {
        try{
            let res = await Tag.find({})
            if (res.length>0){
                ctx.body = {
                    code: 200, 
                    msg: '成功',
                    data: res,
                }
            }else {
                ctx.body = {
                    code: 404, 
                    msg: '无记录',
                    data: res,
                }
            }
        }catch(e){
            ctx.body = {
                code: 500,
                msg: '服务器异常，请稍后再试!'
              }
        }
    },
    async saveTag(ctx,next) {
        let { name } = ctx.request.body;
        try {
            let tag = new Tag({
                name
            })
            let res = await tag.save();
            if (res.id !==null ){
                ctx.body = {
                    code: 200,
                    msg: '创建成功！',
                    data: res
                } 
            } else {
                ctx.body = {
                    code: 500,
                    msg: '服务器异常，请稍后再试!'
                  }
            }
        }catch(e){
            ctx.body = {
                code: 500,
                msg: '服务器异常，请稍后再试!'
              }
        }
    }
}
